<?php
class dashboard extends CI_Controller 
{
		
    function __construct()
	{
		parent::__construct();	
		$this->load->model('product_model');
		$this->load->model('category_model');
		$this->load->language('product','english');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
		$per_page_count 	     = $this->config->item('per_page_count');
		$view 				     = $this->getPath(__FUNCTION__);
		$search                  = $this->input->post('search');
		$where                   = array('deleted' => 0);
		
		$product                  = $this->product_model->getProducts($page, $per_page_count , $like, $where );
		$view['product'] 		  = $product->records;
		$view['cur_page'] 	      = $page;
		$view['title']		      = 'Standards Listing';
		$view['subtitle']	      = 'Standards Lists';
		/************************ Pagination block ***********************************/
		$config['base_url'] 	  = $this->config->item('admin_base_url').'/product/index/';
		$config['total_rows'] 	  = $product->total;
		$config['per_page']       = $per_page_count; //echo  $view['cur_page'];
		$config['cur_page']       = $page /$per_page_count;  //echo $config['cur_page'] ;
		$config['uri_segment'] 	  = 4;
		$config['num_links'] 	  = 3;
		$this->pagination->initialize($config);
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('product_msg'));
		
		$where                    = array('deleted' => 0,'parent_id' => '0');
		$category                 = $this->category_model->getcategories(0,'','', $where );
		$view['category'] 		  = $category->records;
		
		$this->layout->adminview('dashboard', $view);
	
	}
	function add($cur_page = 0)
	{
		$view                    = $this->getPath(__FUNCTION__);
		$view['title']           = "product Details";
		$save                    = $this->input->post('save');
		$view['product'] 	     = array();
		$view['cur_page']        = $cur_page;
		$view['subtitle']	     = 'Add product';
		
		if(!empty($save)) 
		{
			$file                    = $_FILES["picture"]["name"];
			$ext                     = end(explode('.', $file));
			
			$data                 = $this->retrieve->post($this->product_model->fields);
			$data['image']        = $ext;
			$insertStatus         = $this->product_model->add($data);
			$view['product'] 	  = $data;
			
			//print_r($insertStatus);
			/****** image upload ********************************/
			
			//$up_path 				 = $this->config->item('image_path')."products/";
			if(!empty($ext))
			{
			$file_name               = $insertStatus['id'].".".$ext;
    		move_uploaded_file($_FILES["picture"]["tmp_name"],"images/products/" . 	$file_name);
			}
			/****************************************************/
			switch($insertStatus['status'])
			{
				case 1:
					$this->session->set_flashdata('product_msg', 'product_add_success');
					if(!empty($cur_page)) 
						redirect($this->config->item('admin_base_url').'product/index/'.$cur_page,'location');
					else
						redirect($this->config->item('admin_base_url').'product/index/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('product_valid_failed');
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('product_already_exists');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('product_add_failure');
	
					break;
				
				default:
					$view['msg'] 	= $this->lang->line('product_add_failure');
					break;
			}
		} 
		$where                    = array('parent_id'=>0,'deleted' => 0);
		$category                 = $this->category_model->getcategories(0,'','', $where );
		$view['category'] 		  = $category->records;
		$this->layout->adminview('product/product_addedit', $view);
	}

	function edit($id = '', $cur_page='')
	{
		$view                 = $this->getPath(__FUNCTION__);
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base').'product/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$records = $this->product_model->getProducts(0,1, array('id' => $id));
			$product = $records->records[0];
			if(empty($product)) 
			{
				$this->session->set_flashdata('product_msg', 'product_invalid_record');
				redirect($this->config->item('admin_base').'product/index','location');
				exit;
			}
			$view['id']		                = $id;
			
			$view['product']	                = $product;
		}
		
		$view['title']                 = "Edit product ";
		$view['page_title']            = "Edit product";
		$view['subtitle']	           = 'Edit product';
		$save                          = $this->input->post('save');
		if(!empty($save)) 
		{
			$file                    = $_FILES["picture"]["name"];
			$ext                     = end(explode('.', $file));
			
			$data                      = $this->retrieve->post($this->product_model->fields);
			if(!empty($ext))
			{
			$data['image']             = $ext;
			}
			$insertStatus              = $this->product_model->udpate($id, $data);
			
			/************************ image upload **************/
			if(!empty($ext))
			{
			$file_name               = $id.".".$ext;
    		move_uploaded_file($_FILES["picture"]["tmp_name"],"images/products/" . 	$file_name);
			}
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('product_msg', 'product_edit_success');
					redirect($this->config->item('admin_base_url').'product/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('product_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('product_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('product_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('product_edit_failure');
					break;
			}
			$view['product'] 	       = $data;
		}
		$view['cur_page']              = $cur_page; 
		$where                    = array('deleted' => 0,'parent_id'=>0);
		$category                   = $this->category_model->getcategories(0,'','', $where );
		$view['category'] 		  = $category->records;
		$subcategory              = $this->category_model->getcategories(0,'','', array('id'=>$view['product']['category'],'deleted' => 0) );
		$subcategory              = $subcategory->records[0];
		$view['p_id']             = $subcategory['parent_id'];
		$subcategory                   = $this->category_model->getcategories(0,'','', array('parent_id'=>$subcategory['parent_id'],'deleted' => 0) );
		$view['subcategory'] 		  = $subcategory->records;
		$this->layout->adminview('product/product_addedit', $view);
	}
	function info($id = '',$cur_page='')
	{
		
		$product              = $this->product_model->getProducts(0,1,$like, array('id' => $id),'');
		
		$view['product']      = $product->records[0];
		$view['image_path']   = $this->config->item('image_path');
		
		$this->layout->getView('header');
		$this->layout->getView('product_info',$view);
		$this->load->view('footer'); 
		//$this->layout->adminview('admin/product/product_info', $view);
	}
	
	function cancel()
	{
    redirect($this->config->item('admin_base_url').'product/index','location');
		exit;	
	}
	
	function delete($id = '', $cur_page)
	{
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'product/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$deleteStatus = $this->product_model->delete($id);
			switch($deleteStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'product_delete_success');
					redirect($this->config->item('admin_base_url').'product/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	    = $this->lang->line('product_delete_failure');
					break;
			}
		}
		
	}
	function clearFilter()
	{
		$this->session->unset_userdata('filter');
		$this->session->unset_userdata('filter1');
		$this->session->unset_userdata('filter2');
		$this->session->unset_userdata('filter3');
		redirect($this->config->item('admin_base_url').'product/index/','location');
	}
}
?>